package com.hdcircles.autoform.ui.label.activity;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.hdcircles.autoform.R;
import com.hdcircles.autoform.base.BaseActivity;
import com.hdcircles.autoform.model.bean.FormBean;
import com.hdcircles.autoform.presenter.FormListPresenter;
import com.hdcircles.autoform.presenter.contract.FormListContract;
import com.hdcircles.autoform.ui.label.adapter.FormListAdapter;
import com.hdcircles.autoform.util.SnackbarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by hdcircles on 2016/10/18.
 */

public class FormListActivity extends BaseActivity<FormListPresenter> implements FormListContract.View,FormListAdapter.onItemClickListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_form)
    RecyclerView rvForm;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;

    private FormListAdapter adapter;

    private List<FormBean> mList;
    private String id;
    private boolean isCreator;
    private String email;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_form_list;
    }

    @Override
    protected void initEventAndData() {
        setToolBar(toolbar,"已填表单");
        Intent intent=getIntent();
        id = intent.getStringExtra("formId");
        String creatorEmail = intent.getStringExtra("creatorEmail");
        String writerEmail=intent.getStringExtra("writerEmail");
        isCreator=null!= creatorEmail;
        email=null!= creatorEmail ? creatorEmail :writerEmail;
        mList=new ArrayList<>();
        adapter=new FormListAdapter(mList);
        adapter.setOnItemClickListener(this);
        rvForm.setLayoutManager(new LinearLayoutManager(this));
        rvForm.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.getFormData(id,email,isCreator);
            }
        });
//        adapter.setGetUserNameListener(new FormListAdapter.getUserNameListener() {
//            @Override
//            public void getUserName(String time, String receiver, TextView textView) {
//                mPresenter.getUser(receiver,time,textView);
//            }
//        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(true);
                mPresenter.getFormData(id,email,isCreator);
            }
        });
    }

    @Override
    public void showContent(List<FormBean> formBeanList) {
        mList.clear();
        mList.addAll(formBeanList);
        adapter.notifyDataSetChanged();
        refreshLayout.setRefreshing(false);
    }

    @Override
    public void setUserName(String time, String name, TextView textView) {
        textView.setText(time+"\t\t"+name);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId()==android.R.id.home) onBackPressedSupport();
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void showError(String msg) {
//        SnackbarUtil.showShort(toolbar,msg);
        refreshLayout.setRefreshing(false);
    }


    @Override
    public void onItemClick(FormBean form, View view) {
        Intent intent=new Intent(this,FillFormActivity.class);
        Bundle bundle=new Bundle();
        bundle.putParcelable("form",form);
        intent.putExtras(bundle);
        intent.putExtra("isEmpty",false);
        ActivityOptions options=ActivityOptions.makeSceneTransitionAnimation(mContext,view,"shareView");
        startActivity(intent,options.toBundle());
    }
}
